<template>
	<view>
		<!-- 搜索框 -->
		<uni-search-bar @confirm="search"></uni-search-bar>
		<!-- 轮播图 -->
			<swiper circular autoplay :interval="3000" :duration="500" indicator-dots style="height: 350rpx;"
				indicator-color="rgba(255, 255, 255, 0.6)" indicator-active-color="#3CB371">
				<swiper-item v-for="item in banners" :key="item.id">
					<view @click="goToBannerBusiness(item.businessId)">
						<image :src="item.img" alt="" mode="widthFix" style="width: 100%;" />
					</view>
				</swiper-item>
			</swiper>
	
		
		<view style="padding: 20rpx;">
			<!-- 公告 -->
			<view>
				<uni-notice-bar 
				show-icon 
				speed="20"
				scrollable="true"
				color="#2979FF"
				background-color="#EAF2FF"
				v-if="notice"
				:text="notice"
				/>
			</view>
			<!-- 商家分类 -->
			<view class="box" style="margin: 20rpx 0;"> 
				<view style="display: flex;">
					<view class="category-item" @click="goToBusiness('美食外卖')">
						<image src="../../static/icons/外卖美食.png" mode="widthFix" style="width: 50%;"></image>
						<text>美食外卖</text>
					</view>
					<view class="category-item" @click="goToBusiness('商超便利')">
						<image src="../../static/icons/超商便利.png" mode="widthFix" style="width: 50%;"></image>
						<text>超商便利</text>
					</view>
					<view class="category-item" @click="goToBusiness('水果鲜花')">
						<image src="../../static/icons/水果鲜花.png" mode="widthFix" style="width: 50%;"></image>
						<text>水果鲜花</text>
					</view>
					<view class="category-item" @click="goToBusiness('买药')">
						<image src="../../static/icons/买药.png" mode="widthFix" style="width: 50%;"></image>
						<text>买药</text>
					</view>
					<view class="category-item" @click="goToBusiness('甜品饮品')">
						<image src="../../static/icons/甜品饮品.png" mode="widthFix" style="width: 50%;"></image>
						<text>甜品饮品</text>
					</view>
				</view>
			</view>
		
			<!-- 商家列表 -->
			<view>
				<!-- 热门商家 -->
				<view>
					<uni-card :is-shadow="false" is-full="true">
						<text style="color: skyblue;font-size: 32rpx; font-weight: bold; ">热门商家</text>
					</uni-card>
				</view>
				
				<!-- 商家列表 -->
					<view>
						<view  v-for="item in businessList" :key="item.id" style="margin-top: 20rpx;">
							<uni-card isFull="true">
								<view style="display: flex; grid-gap: 30rpx; margin-bottom: 10rpx;" @click="goToDetail(item.id)">
									<view style="width: 30%;">
										<image :src="item.avatar" style="width: 100%; height: 200rpx; border-radius: 10rpx; display: block;"></image>
									</view>
									<view style="flex: 1; display: flex; flex-direction: column; justify-content: space-between; grid-gap: 10rpx;">
										<view style="font-size: 36rpx; font-weight: bold;">{{ item.name }}</view>
												
										<view style="display: flex;  color: #666; ">
											<view style="flex: 1;">
												<text style="color: #ff9800; font-weight: bold;">{{ item.score }}分</text>
												<text style="margin-left: 10rpx;">已售:{{ item.nums }}</text>
											</view>
											<view style="flex: 1; text-align: right;">30分钟内送达</text>
											</view>
										</view>
										
										<view style="color: skyblue;">免配送费</view>
										
										<view style="background-color: #ffd281; color: brown; border-radius: 4rpx; width: fit-content;
											padding: 0 8rpx;">{{ item.info || '商家介绍' }}</view>
												
									</view>
								</view>
							</uni-card>
						</view>
								
					</view>
			
			
			<!-- 商家列表结束 -->
			</view>
			
		</view>
	</view>
	

	
	
	
</template>

<script>
	export default {
		data() {
			return {
				notice: '暂无通知',
				noticeList: [],
				businessList: [],
				banners:[]
			}
		},
		
		onShow() {
			this.load();
		},
		
		methods: {
			goToBannerBusiness(businessId){
				uni.navigateTo({
					url:'/pages/detail/detail?businessId='+businessId
				})
			},
			goToBusiness(type){
				uni.navigateTo({
					url:'/pages/categoryBusiness/categoryBusiness?type='+type
				})
			},
			search(res){
				let value = res.value
				uni.navigateTo({
					url:'/pages/search/search?name=' + value
				})
			},
			load(){
				//获取轮播图数据
				this.$request.get('/banner/selectAll').then(res => {
					console.log(res.data)
					this.banners = res.data || []
				})
				
				//获取公告数据
				this.$request.get('/notice/selectAll').then(res => {
					this.noticeList = res.data || []
					if(this.noticeList.length){
						let i = 0;
						this.notice = this.noticeList[i++].content
						if(this.noticeList.length > 1){
							setInterval(() => {
								if(i === this.noticeList.length){
									i = 0
								}
								this.notice = this.noticeList[i++].content
							},50000)
						}
					}
				})
				
				//获取商家数据
				this.$request.get('/business/selectAll',{
						status:"通过"
				}).then(res => {
						this.businessList = res.data || []
				})
				
			},
			
			goToDetail(businessId){
				uni.navigateTo({
					url: '/pages/detail/detail?businessId=' + businessId
				})
			}
			
		}
	}
</script>

<style>
	.category-item {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		align-items: center;
		grid-gap: 10rpx;
	}
</style>
